/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rukopisyimport.foxml;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Rehanek (rehan@mzk.cz)
 */
public class PageTitleResolver {

    private static final Logger logger = Logger.getLogger(PageTitleResolver.class.getName());
//    private Map<String, String> specialPageTitleMap = new HashMap<String, String>();
    private Map<String, String> specialPageTypeMap = new HashMap<String, String>();

    public PageTitleResolver() {
//        specialPageTitleMap.put("FC", "přední deska vazby");
//        specialPageTitleMap.put("FS", "přední přídeští");
//        specialPageTitleMap.put("SP", "hřbet");
//        specialPageTitleMap.put("BS", "zadní přídeští");
//        specialPageTitleMap.put("BC", "zadní deska vazby");

        specialPageTypeMap.put("FC", "FrontCover");
        specialPageTypeMap.put("FS", "FrontEndSheet");
        specialPageTypeMap.put("SP", "Spine");
        specialPageTypeMap.put("BS", "BackEndSheet");
        specialPageTypeMap.put("BC", "BackCover");
    }

//    public String getPageTitle(String originalTitle) {
//        String specialPageTitle = getSpecialPageTitle(originalTitle);
//        if (specialPageTitle != null) {
//            return specialPageTitle;
//        }
//        Integer numberPageTitle = getNumberPageTitle(originalTitle);
//        if (numberPageTitle != null) {
//            return numberPageTitle.toString();
//        }
//        return originalTitle;
//    }

    public String getPageType(String originalTitle) {
        if (isSpecialPageTitle(originalTitle)) {
            return getSpecialPageType(originalTitle);
//        } else if (isNormalPageTitle(originalTitle)) {
//            return "NormalPage";
        } else {
            return "NormalPage";
        }
    }

    private boolean isSpecialPageTitle(String originalTitle) {
        return getSpecialPageType(originalTitle) != null;
    }

//    private boolean isNormalPageTitle(String originalTitle) {
//        return getNumberPageTitle(originalTitle) != null;
//    }

//    public Integer getNumberPageTitle(String originalTitle) {
//        try {
//            Integer pageNumber = Integer.parseInt(originalTitle);
//            return pageNumber;
//        } catch (NumberFormatException e) {
//            Integer convertedFromRoman = romanToArabic(originalTitle);
//            if (convertedFromRoman != null) {
//                return convertedFromRoman;
//            } else {
//                return null;
//            }
//        }
//    }

//    private String getSpecialPageTitle(String originalTitle) {
//        return specialPageTitleMap.get(originalTitle);
//    }

    public String getSpecialPageType(String originalTitle) {
        return specialPageTypeMap.get(originalTitle);
    }

    /**
     * 
     * @param romanNumeral
     * @return arabic representation of romanNumeral or null if string is not roman numeral
     */
    private Integer romanToArabic(String romanNumeral) {
        try {
            RomanNumeral ro = new RomanNumeral(romanNumeral);
            return Integer.valueOf(ro.toInt());
        } catch (NumberFormatException ex) {
            logger.log(Level.SEVERE, "{0}: {1}", new Object[]{romanNumeral, ex.getMessage()});
            return null;
        }
    }
}
